package org.springblade.modules.client;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.modules.material.entity.TrainArchiveEntity;
import org.springblade.modules.material.entity.TrainArchiveUserEntity;
import org.springblade.modules.material.service.ITrainArchiveService;
import org.springblade.modules.material.service.ITrainArchiveUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

/**
 * 学习资料相关功能接口
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-client/trainArchive")
@Api(value = "客户端资线下实训接口" ,tags ="客户端线下实训相关功能接口" )
public class ClientTrainArchiveController {

	private final ITrainArchiveService trainArchiveService;
	private final ITrainArchiveUserService trainArchiveUserService;


	/**
	 * 资料分享分页
	 */
	@GetMapping("/train-list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "线下实训分页列表", notes = "")
	public R trainingMaterialList(TrainArchiveEntity entity, Query query) {
		LambdaQueryWrapper<TrainArchiveUserEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(TrainArchiveUserEntity::getUserId, AuthUtil.getUserId());
		IPage<TrainArchiveUserEntity> pages = trainArchiveUserService.page(Condition.getPage(query), lambdaQueryWrapper);

		if (CollectionUtil.isEmpty(pages.getRecords()))return R.success("暂无数据");
		LambdaQueryWrapper<TrainArchiveEntity> coursewarQueryWrapper=new LambdaQueryWrapper<>();
		coursewarQueryWrapper.in(TrainArchiveEntity::getId,pages.getRecords().stream().map(TrainArchiveUserEntity::getArchiveId).collect(Collectors.toList()));
		return R.data(trainArchiveService.page(Condition.getPage(query),coursewarQueryWrapper));
	}


}
